import pygame
import script.game_functions as gf

from script.settings import Settings
from script.ship import Ship
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from scoreboard import ScoreBoard


def run_game():
    # 初始化
    pygame.init()
    # 设置标题名
    pygame.display.set_caption("Alien Invasion")
    # 初始化设置
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    background = pygame.image.load('../image/background.png')
    # 创建Play按钮
    play_button = Button(ai_settings, screen, "PLAY")
    # 创建飞船
    ship = Ship(ai_settings, screen)
    # 存储子弹的编组
    bullets = Group()
    # 创建一个敌人
    aliens = Group()
    # 创造敌人组
    gf.create_fleet(ai_settings, screen, ship, aliens)
    # 创建一个存储游戏统计信息的实例，计分
    stats = GameStats(ai_settings)
    scoreboard = ScoreBoard(ai_settings, screen, stats)

    # 开始游戏的循环
    while True:
        # 按键事件
        gf.check_events(ai_settings, screen, stats, scoreboard, play_button, ship, aliens, bullets)
        # 根据游戏状态判断是否开始游戏
        if stats.game_active:
            # 飞船移动
            ship.update()
            # 发射子弹
            gf.update_bullets(ai_settings, screen, stats, scoreboard, ship, aliens, bullets)
            # 敌人移动
            gf.update_aliens(ai_settings, stats, scoreboard, screen, ship, aliens, bullets)
        # 更新页面事件
        gf.update_screen(ai_settings, screen, stats, scoreboard, ship, bullets, aliens, background, play_button)


run_game()
